library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Creating Figure 6.9
# Wave 2

# Create binaries for Q20DD2 - Active duty Gen/Adm to crit President
summary(factor(df$Q20DD))

df$Q20DD2[df$Q20DD < 3] <- 1
df$Q20DD2[df$Q20DD > 2 & df$Q20DD < 77] <- 0
summary(factor(df$Q20DD2))

# P_Q20 is Criticize conditions: 1 = Crit Trump; 2 = Crit Obama; 3 = Crit POTUS
summary(factor(df$P_Q20))

# Create Confidence binary - 0 not confident; 1 confident
summary(factor(df$Q11))
df$Q11T <- NA
df$Q11T[df$Q11 < 3] <- 1
df$Q11T[df$Q11 > 2 & df$Q11 < 77] <- 0

# Drop NA
df2 <- df[!is.na(df$Q20DD2),]
df2 <- df2[!is.na(df2$party),]
df2 <- df2[!is.na(df2$Q11T),]

# Creating weighted survey design objects
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )


# Wave 2 - Not Confident
# Criticize Trump
w2t_d_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==0 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2t_i_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==1 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2t_r_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==2 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))

# Criticize Obama
w2o_d_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==0 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2o_i_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==1 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2o_r_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==2 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))

# Criticize POTUS
w2p_d_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==0 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2p_i_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==1 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))
w2p_r_n <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==2 & w2_design$variables$Q11T == 0],method='me',df=degf(w2_design))

# Wave 2 - Confident
# Criticize Trump
w2t_d_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==0 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2t_i_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==1 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2t_r_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==1 & w2_design$variables$party==2 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))

# Criticize Obama
w2o_d_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==0 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2o_i_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==1 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2o_r_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==2 & w2_design$variables$party==2 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))

# Criticize POTUS
w2p_d_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==0 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2p_i_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==1 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))
w2p_r_c <- svyciprop(~I(Q20DD2),w2_design[w2_design$variables$P_Q20==3 & w2_design$variables$party==2 & w2_design$variables$Q11T == 1],method='me',df=degf(w2_design))


# Wave 2 - Not confident
pt.est1 <- c(w2t_d_n[1],w2t_i_n[1],w2t_r_n[1],2,
             w2o_d_n[1],w2o_i_n[1],w2o_r_n[1],2,
             w2p_d_n[1],w2p_i_n[1],w2p_r_n[1])
ci.low1 <- c(attr(w2t_d_n,'ci')[1],attr(w2t_i_n,'ci')[1],attr(w2t_r_n,'ci')[1],1.99,
             attr(w2o_d_n,'ci')[1],attr(w2o_i_n,'ci')[1],attr(w2o_r_n,'ci')[1],1.99,
             attr(w2p_d_n,'ci')[1],attr(w2p_i_n,'ci')[1],attr(w2p_r_n,'ci')[1])
ci.high1 <- c(attr(w2t_d_n,'ci')[2],attr(w2t_i_n,'ci')[2],attr(w2t_r_n,'ci')[2],2.1,
              attr(w2o_d_n,'ci')[2],attr(w2o_i_n,'ci')[2],attr(w2o_r_n,'ci')[2],2.1,
              attr(w2p_d_n,'ci')[2],attr(w2p_i_n,'ci')[2],attr(w2p_r_n,'ci')[2])
plot.labs <- c(" ","Criticize\nTrump","  ","        ",
               "   ","Criticize\nObama","    ","          ",
               "     ","Criticize\nPOTUS", "      ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w2n <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red','gray10',
                                        'blue','gray50','red','gray10',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(0,0.95)+
  ylab("Respondent Agreement (%)")+
  xlab("")+
  ggtitle("Not Confident")+
  theme_bw()


# Wave 2 - Confident
pt.est1 <- c(w2t_d_c[1],w2t_i_c[1],w2t_r_c[1],2,
             w2o_d_c[1],w2o_i_c[1],w2o_r_c[1],2,
             w2p_d_c[1],w2p_i_c[1],w2p_r_c[1])
ci.low1 <- c(attr(w2t_d_c,'ci')[1],attr(w2t_i_c,'ci')[1],attr(w2t_r_c,'ci')[1],1.99,
             attr(w2o_d_c,'ci')[1],attr(w2o_i_c,'ci')[1],attr(w2o_r_c,'ci')[1],1.99,
             attr(w2p_d_c,'ci')[1],attr(w2p_i_c,'ci')[1],attr(w2p_r_c,'ci')[1])
ci.high1 <- c(attr(w2t_d_c,'ci')[2],attr(w2t_i_c,'ci')[2],attr(w2t_r_c,'ci')[2],2.1,
              attr(w2o_d_c,'ci')[2],attr(w2o_i_c,'ci')[2],attr(w2o_r_c,'ci')[2],2.1,
              attr(w2p_d_c,'ci')[2],attr(w2p_i_c,'ci')[2],attr(w2p_r_c,'ci')[2])
plot.labs <- c(" ","Criticize\nTrump","  ","        ",
               "   ","Criticize\nObama","    ","          ",
               "     ","Criticize\nPOTUS", "      ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w2c <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red','gray10',
                                        'blue','gray50','red','gray10',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(0,.95)+
  ylab("Respondent Agreement (%)")+
  xlab("")+
  ggtitle("Confident")+
  theme_bw()

grid.arrange(plot_w2n, plot_w2c, nrow=1, ncol=2)